const app = getApp();
var url = app.data.url;
import Dialog from '@vant/weapp/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    merchantId: '',
    radio: '0',
    loginType: 0,
    uuid: '',
    queryType: ''
  },
  onChange(event) {
    this.setData({
      radio: event.detail
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let merchantId = options.merchantId;
    let _that = this;
    _that.setData({
      merchantId: merchantId
    })
    wx.setNavigationBarTitle({
      title: "查看付费方案"
    })
  },
  agreeGetUser: function(options) {
    let _that = this;
    wx.login({
      success(res) {
        if (res.code) {
          //发起网络请求
          wx.request({
            url: url + '/WeChatLoginApi/login',
            data: {
              nickName: options.detail.userInfo.nickName,
              code: res.code,
              img: options.detail.userInfo.avatarUrl,
              sex: options.detail.userInfo.gender,
              city: options.detail.userInfo.city,
              province: options.detail.userInfo.province,
              country: options.detail.userInfo.country
            },
            method: 'POST',
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            success: function(res) {
              if (res.data.code == 200) {
                // 查询商家
                let uuid = res.data.data;
                wx.request({
                  url: url + '/MerchantPaySystemApi/queryMerchantByIdNew',
                  data: {
                    uuid: uuid
                  },
                  method: 'POST',
                  header: {
                    'content-type': 'application/x-www-form-urlencoded'
                  },
                  success: function(item) {
                    console.log(item.data.data);
                    if (item.data.code == 200) {
                      // 查询商家
                      wx.showToast({
                        title: "登录成功!",
                        icon: "success",
                        duration: 2000
                      })
                      wx.setStorage({
                        key: "loginType",
                        data: 1
                      })
                      _that.setData({
                        loginType: 1,
                        uuid: item.data.data.uuid,
                        merchantId: item.data.data.id,
                        queryType: item.data.data.type
                      })
                    } else {
                      wx.showToast({
                        title: item.data.msg,
                        icon: "none",
                        duration: 2000
                      })
                    }
                  },
                  fail: function(res) {
                    console.info('submit fail');
                  },
                  complete: function(res) {
                    console.info('submit complete');
                  }
                })

              } else {
                wx.showToast({
                  title: res.data.msg,
                  icon: "none",
                  duration: 2000
                })
              }
            },
            fail: function(res) {
              console.info('submit fail');
            },
            complete: function(res) {
              console.info('submit complete');
            }
          })
        } else {
          console.log('登录失败！' + res.errMsg)
        }

      }
    })
  },
  /**
   * 支付
   */
  pay: function() {
    let _that = this;
    let merchantId = _that.data.merchantId;
    let uuid = _that.data.uuid;
    let paymentType = _that.data.radio;
    console.log(paymentType);
    let payMoney = 68;
    if (paymentType == 0){
      payMoney = 68;
    } else if (paymentType == 2){
      payMoney = 340;
    } else {
      payMoney = 408;
    } 
    wx.request({
      url: url + '/MerchantPaySystemApi/insertMerchantOrder',
      data: {
        merchantId: merchantId,
        bUuid: uuid,
        payMoney: payMoney,
        paymentType: paymentType
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        if (res.data.code == 200) {
          console.log(res);
          let mchOrder = res.data.data;
          let id = mchOrder.id;
          let bUuid = mchOrder.bUuid;
          let orderId = mchOrder.orderId;
          let payMonty = mchOrder.payMoney;
          wx.request({
            url: url + '/WeChatPayApi/prePay',
            data: {
              uuid: uuid,
              orderId: orderId,
              merchantId: merchantId,
              pms: payMoney
            },
            method: 'POST',
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            success: function (res) {
              if (res.data.code == 200) {
                let item = res.data.data;
                let wxPaymentPojo = item.wxPaymentParamPojo;
                // 发起支付
                wx.requestPayment({
                  'timeStamp': wxPaymentPojo.timeStamp,
                  'nonceStr': wxPaymentPojo.nonceStr,
                  'package': wxPaymentPojo.packageStr,
                  'signType': 'MD5',
                  'paySign': wxPaymentPojo.paySign,
                  'success': function (res) {
                    console.log("支付成功！！！！！");
                    // 支付成功, 新增一条支付单记录
                    let jsonParam = JSON.stringify(item.stringStringMap);
                    wx.request({
                      url: url + '/WeChatPayApi/insertWxPay',
                      data: {
                        jsonParam: jsonParam
                      },
                      method: 'POST',
                      header: {
                        'content-type': 'application/x-www-form-urlencoded'
                      },
                      success: function (res) {
                        if (res.data.code == 200) {
                          // 支付成功, 更新订单状态
                          wx.request({
                            url: url + '/MerchantPaySystemApi/editMerchantOrder',
                            data: {
                              merchantId: merchantId,
                              bUuid: uuid,
                              status: 'PAY',
                              id: id,
                              paymentType: paymentType
                            },
                            method: 'POST',
                            header: {
                              'content-type': 'application/x-www-form-urlencoded'
                            },
                            success: function (res) {
                              if (res.data.code == 200) {
                                // 支付成功 设置loginType = 2
                                _that.setData({
                                  loginType: 2
                                })
                                console.log("发送模板消息")
                                // 发送订阅通知
                                wx.request({
                                  url: url + '/WeChatPayApi/toSendMessage',
                                  data: {
                                    orderId: orderId,
                                    wxUserUuid: uuid
                                  },
                                  method: 'POST',
                                  header: {
                                    'content-type': 'application/x-www-form-urlencoded'
                                  }
                                })
                              } else {
                                wx.showToast({
                                  title: res.data.msg,
                                  icon: "none",
                                  duration: 2000
                                })
                              }
                            }
                          })

                        } else {
                          wx.showToast({
                            title: res.data.msg,
                            icon: "none",
                            duration: 2000
                          })
                        }
                      }
                    })

                  },
                  'fail': function (res) {
                    console.log("支付失败!");
                    console.log(res);
                  },
                  'complete': function (res) {
                    console.log("方法调用结束");
                    console.log(res);
                  }
                })
              } else {
                wx.showToast({
                  title: res.data.msg,
                  icon: "none",
                  duration: 2000
                })
              }

            },
            fail: function (res) {
              console.info('submit fail');
            },
            complete: function (res) {
              console.info('submit complete');
            }
          })


        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none",
            duration: 2000
          })
        }
      }
    })


    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})